﻿using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Model.Page;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Collections.Generic;


namespace Hospital.WebApi.Controllers
{


    [Route("api/[controller]")]
    [ApiController]
    [Authorize]

    public class DoctorController : ControllerBase
    {
        private readonly IDoctorService _iDoctorService;

        public DoctorController(IDoctorService iDoctorService)
        {
            _iDoctorService = iDoctorService;
        }

        // Get a doctor by ID
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResult>> GetDoctorById(int id)
        {
            try
            {
                var doctor = await _iDoctorService.FindAsync(id);
                if (doctor == null) return ApiResultHelper.Error("查询为空");
                return ApiResultHelper.Success(doctor);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("查询时出现错误");
            }
        }

        // 分页查询
        [HttpGet("page")]
        public async Task<ActionResult<ApiResult>> GetDoctorsByPage([FromQuery] PageRequest pageRequest)
        {
            RefAsync<int> total = 0;
            var doctors = await _iDoctorService.QueryAsync(pageRequest, total);
            return ApiResultHelper.Success(doctors, total);
        }

        // Get all doctor
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetDoctors()
        {
            try
            {
                var doctors = await _iDoctorService.QueryAsync();
                if (doctors != null) { return ApiResultHelper.Success(doctors); }
                else
                {
                    return ApiResultHelper.Error("医生数据为空");
                }
            }
            catch
            {
                return ApiResultHelper.Error("获取医生失败,服务器出现错误");
            }
        }

        // Create a new doctor
        [HttpPost]
        public async Task<ActionResult<ApiResult>> CreateDoctor([FromBody] Doctor doctor)
        {
            try
            {
                bool b = await _iDoctorService.CreateAsync(doctor);
                if (b) return ApiResultHelper.Success("创建成功");
                return ApiResultHelper.Error("添加医生失败");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("出现错误");
            }
        }

        // Update doctor details
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> PutDoctor(int id, [FromBody] Doctor doctor)
        {
            try
            {
                if (id == doctor.Id)
                {
                    bool b = await _iDoctorService.EditAsync(doctor);
                    if (b) return ApiResultHelper.Success("修改成功");
                    return ApiResultHelper.Error("修改失败");
                }
                else
                {
                    return ApiResultHelper.Error("id:" + id + ",DoctorID:" + doctor.Id);
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("修改医生信息出现错误");
            }
        }

        // 修改医生信息
        [HttpPatch("{id}")]
        public async Task<ActionResult<ApiResult>> PatchDoctor(int id, [FromBody] Doctor doctor)
        {
            if (id != doctor.Id)
            {
                return BadRequest();
            }
            bool result = await _iDoctorService.EditAsync(doctor);
            if (result)
            {
                return NoContent(); // Successfully updated
            }
            return NotFound(); // Doctor not found
        }

        // Delete a doctor by ID
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteDoctor(int id)
        {
            try
            {
                bool result = await _iDoctorService.DeleteAsync(id);
                if (result)
                {
                    return ApiResultHelper.Success("删除成功"); // Successfully deleted
                }
                return ApiResultHelper.Error("删除失败"); // Doctor not found

            }
            catch (Exception)
            {
                return ApiResultHelper.Success("删除时出现错误");
            }
        }
    }
}